
//
// Windows Scripting Host
//

writeFile = function(filename, text) {
	var file;
	writeCounter++;
	try {
		file = fso.CreateTextFile(filename, overwriteIfExist, openAsASCII);
		file.WriteLine(text);
	} catch(e) {
		try {
			if (file && typeof file.Close != 'undefined')
				file.Close();
			file = null;
			file = fso.CreateTextFile(filename, overwriteIfExist, openAsUnicode);
			file.WriteLine(text);
			echo('Warning: file ' + filename + ' was saved as Unicode.');
		} catch(e) {
			echo('Unable to write to ' + filename + '.');
			writeCounter--;
		}
	}
	if (file && typeof file.Close != 'undefined')
		file.Close();
}

binToHex = function(binStr) {
	var xmldom = new ActiveXObject("Microsoft.XMLDOM"); 
	var binObj= xmldom.createElement("binObj"); 
	binObj.dataType = 'bin.hex'; 
	binObj.nodeTypedValue = binStr; 
	return String(binObj.text); 
}

hexToBin = function(hexStr) {
	var xmldom = new ActiveXObject("Microsoft.XMLDOM"); 
	var binObj= xmldom.createElement("binObj"); 
	binObj.dataType = 'bin.hex'; 
	binObj.text = hexStr;
	return binObj.nodeTypedValue; 
}

var hexChars = {};
(function(){
	for (var i = 0; i < 10; i++)
		hexChars[String(i)] = i;
	for (var i = 0; i < 6; i++) {
		hexChars[String.fromCharCode('a'.charCodeAt(0) + i)] = i + 10;
		hexChars[String.fromCharCode('A'.charCodeAt(0) + i)] = i + 10;
	}
})();

hexToInt = function(hexStr) {
	var data = [];
	var zero = '0'.charCodeAt(0);
	for (var i = 0, j = 0; i < hexStr.length; i += 2, j++)
		data[j] = (hexChars[hexStr.charAt(i)] << 4) + hexChars[hexStr.charAt(i+1)];
	return data;
}

intToHex = function(intArr) {
	var data = '';
	for (var i = 0; i < intArr.length; i++) {
		if (intArr[i] < 16)
			data += '0';
		data += intArr[i].toString(16);
	}
	return data;
}

readBinFile = function(filename) {
	var adTypeBinary = 1;
	var BinaryStream = new ActiveXObject("ADODB.Stream");
	BinaryStream.Type = adTypeBinary;
	BinaryStream.Open();
	BinaryStream.LoadFromFile(filename);
	var data = BinaryStream.Read();
	data = hexToInt(binToHex(data));
	return data;
}

writeBinFile = function(filename, data) {
	writeCounter++;
	try {
		var adTypeBinary = 1, adTypeText = 2, adSaveCreateOverWrite = 2;
		var BinaryStream = new ActiveXObject("ADODB.Stream");
		BinaryStream.Type = adTypeBinary;
		BinaryStream.Open();
		
		BinaryStream.Write(hexToBin(intToHex(data)));
		BinaryStream.SaveToFile(filename, adSaveCreateOverWrite);
		BinaryStream.Close();
	} catch(e) {
		echo('Unable to write binary file ' + filename + '. Error msg: ' + e.message);
		writeCounter--;
	}
}

listFiles = function(folder, extension, prefix) {
	var vec = [], ext, pre;
	if (arguments.length > 1) rx = new RegExp('\\.' + extension + '$');
	if (arguments.length > 2) pre = new RegExp('^' + prefix + '\\.');
	var e = new Enumerator(fso.getFolder(folder).files);
	for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
		var name = e.item().name;
		if (arguments.length < 2 || (rx.test(name) && (arguments.length < 3 || pre.test(name))) )
			vec.push(name);
	}
	return vec;
}

getFileSize = function(filePath) {
	var m = filePath.match(/\/([^\/]+)$/);
	var filename = m && m[1] || filePath;
	var folder = filePath.substr(0, m.index) + '/';
	var e = new Enumerator(fso.getFolder(folder).files);
	for (e.moveFirst(); ! e.atEnd(); e.moveNext())
		if (e.item().name == filename)
			return e.item().size;
	return null;
}

ensurePathExists = function(filepath) {
	var folders = filepath.split('/');
	if (!folders[folders.length-1]) folders.pop();
	var now = '';
	for (var i = 0; i < folders.length; i++) {
		if (i) now += '\\'
		now += folders[i];
		if (!fso.folderExists(now))
			fso.createFolder(now);
	}
}

copyFile = function(source, target) {
	fso.CopyFile(source, target);
}

fileExists = function(filename) {
	return fso.FileExists(filename);
}

deleteFile = function(filename) {
	fso.DeleteFile(filename);
}

copyFolder = function(source, target) {
	fso.CopyFolder(source, target);
}

args = [];
for (var i = 0; i < WScript.Arguments.length; i++)
	args[i] = WScript.Arguments.Item(i);
